﻿<!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/Dtd/xhtml1-transitional.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Text to HTML converter</title>
	<style type="text/css">
		body{
			background-color:#ccc;
			font-family: Verdana, Arial, Sans-Serif;
			font-size: 12px;
		}
	</style>
	<script type="text/javascript">
		function convertText(){
			var lineWrapper = document.getElementById("tbLineWrapper").value,
				allWrapper = document.getElementById("tbAllWrapper").value,
				insertNbsp = document.getElementById("cbInsertNbsp").checked
				insertNDash = document.getElementById("cbInsertNDash").checked;
			var txt = document.getElementById("srcText").value;
			txt = txt.replace(/\&/g, "&amp;")
				.replace(/\>/g, "&gt;")
				.replace(/\</g, "&lt;")
				.replace(/\"/g, "&quot;")
				.replace(/\«/g, "&laquo;")
				.replace(/\»/g, "&raquo;")
				.replace(/\'/g, "&apos;")
				.replace(/–/g, "-"); // N-Dash
				
			if(lineWrapper.length){
				txt = txt.replace(/[\n\r]+/g, "</"+lineWrapper+">\n"+(allWrapper.length?"\t":"")+"<"+lineWrapper+">");
				txt = (allWrapper.length?"\t":"")+"<"+lineWrapper+">"+txt+"</"+lineWrapper+">";
			}
			
			if(insertNDash) txt = txt.replace(/ - /g, " &ndash; ").replace(/ - /g, " &ndash; ");
			
			
			txt = txt.replace(/ +/g, " ");
				
			if(insertNbsp){
				txt = txt.replace(/(\s|(&nbsp;))([a-zа-я][a-zа-я\.]?)\s/ig, "$1$3&nbsp;")
				.replace(/(\s|(&nbsp;))([a-zа-я][a-zа-я\.]?)\s/ig, "$1$3&nbsp;");
			}
			
			if(allWrapper.length)
				txt = "<"+allWrapper+">\n"+txt+"\n</"+allWrapper+">";
			var destFld = document.getElementById("destText");
			destFld.value = txt;
		}
	</script>
</head>
<body>
	<h1>Text to HTML converter</h1>
	<textarea id="srcText" style="width:700px; height:300px;"></textarea>
	<div>
		<input type="button" value="Convert" onclick="convertText()"/> , 
		<div>wrapping lines with <input type="text" value="p" id="tbLineWrapper" style="width:35px;"/> tag</div>
		<div>wrapping all with <input type="text" value="" id="tbAllWrapper" style="width:35px;"/> tag</div>
		<div>
			<input type="checkbox" id="cbInsertNbsp" checked="true"/> insert non-break spaces
			<input type="checkbox" id="cbInsertNDash" checked="true"/> insert n-dashes
		</div>
	</div>
	<textarea id="destText" readonly="true" style="width:700px; height:300px;"></textarea>
</body>
</html>